define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {
    var Controller = {
        index: function () {
            var linenum = 'today_line';
            var picnum1 = 'today_pic';
            var picnum2 = 'today_pic2';
            var diagramChart = Echarts.init(document.getElementById('diagram'));
            var piechartChart = Echarts.init(document.getElementById('pie-chart'));
            //获取今天时间
            function getNowFormatDate() {
                var date = new Date();
                var seperator1 = "-";
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = year + seperator1 + month + seperator1 + strDate;
                return currentdate;
            }
            //今天开始 - 结束时间
            function startUnix($date) {
                return(new Date(Date.parse($date.replace(/-/g, "/")))).getTime() / 1000;
            }
            function endUnix($date,n,m) {
                return new Date().setTime(Date.parse($date.replace(/-/g, "/")) / 1000 + 24 * n * 60 * 60 + m);
            }
            var todayTime = getNowFormatDate();
            //初始化时间
            var startline = startUnix(todayTime);
            var endline = endUnix(todayTime,1,-1);
            
            var startPic = startUnix(todayTime);
            var endPic = endUnix(todayTime,1,-1);

            var startPic2 = startUnix(todayTime);
            var endPic2 = endUnix(todayTime,1,-1);
            // console.log(startUnix(todayTime));
            // console.log(endUnix(todayTime,1,-1));

            // console.log('昨天开始'+endUnix(todayTime,-1,0));
            // console.log('昨天结束'+endUnix(todayTime,0,-1));

            // console.log('7天开始'+endUnix(todayTime,-6,0));
            // console.log('7天结束'+endUnix(todayTime,0,-1));
            
            // console.log('30天开始'+endUnix(todayTime,-29,0));
            // console.log('30天结束'+endUnix(todayTime,0,-1));

            //初始化图表
            restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
            //根据时间重置图表
            function restEharts(startline,endline,startPic,endPic,startPic2,endPic2){
                dashtime('/admin/index/home?start_time_line=' + startline +'&end_time_line=' + endline + '&start_time_pic=' + startPic + '&end_time_pic=' + endPic + '&start_time_pic2=' + startPic2 + '&end_time_pic2='+ endPic2 );
            }
            //根据选中时间类型切换

            function checkType(str){
                if(str=="free1"){
                    $('.layui-form1').show();
                    linenum='today_line';
                }else{
                    $('#start,#stop').val('');
                    $('.layui-form1').hide();
                    if(str == 'today_line'){
                        startline = startUnix(todayTime);
                        endline = endUnix(todayTime,1,-1)
                    }else if(str == 'last_today_line'){
                        startline = endUnix(todayTime,-1,0);
                        endline = endUnix(todayTime,0,-1);
                    }else if (str == 'seven_day_line'){
                        startline = endUnix(todayTime,-6,0);
                        endline = endUnix(todayTime,0,-1);
                    }else if(str == 'thirty_day_line'){
                        startline = endUnix(todayTime,-29,0);
                        endline = endUnix(todayTime,0,-1);
                    }
                    restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                }
            }
            //线形图
            function checkType1(str){
                if(str=="free2"){
                    $('.layui-form2').show();
                    picnum1='today_pic';
                }else{
                    $('#start1,#stop1').val('');
                    $('.layui-form2').hide();

                    if(str == 'today_pic'){
                        startPic = startUnix(todayTime);
                        endPic = endUnix(todayTime,1,-1)
                    }else if(str == 'last_today_pic'){
                        startPic = endUnix(todayTime,-1,0);
                        endPic = endUnix(todayTime,0,-1);
                    }else if (str == 'seven_day_pic'){
                        startPic = endUnix(todayTime,-6,0);
                        endPic = endUnix(todayTime,0,-1);
                    }else if(str == 'thirty_day_pic'){
                        startPic = endUnix(todayTime,-29,0);
                        endPic = endUnix(todayTime,0,-1);
                    }
                    restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                }
            }
            //饼状图
            function checkType2(str){
                if(str=="free3"){
                    $('.layui-form3').show();
                    picnum2='today_pic2';
                }else{
                    $('#start2,#stop2').val('');
                    $('.layui-form3').hide();
                    if(str == 'today_pic2'){
                        startPic2 = startUnix(todayTime);
                        endPic2 = endUnix(todayTime,1,-1)
                    }else if(str == 'last_today_pic2'){
                        startPic2 = endUnix(todayTime,-1,0);
                        endPic2 = endUnix(todayTime,0,-1);
                    }else if (str == 'seven_day_pic2'){
                        startPic2 = endUnix(todayTime,-6,0);
                        endPic2 = endUnix(todayTime,0,-1);
                    }else if(str == 'thirty_day_pic2'){
                        startPic2 = endUnix(todayTime,-29,0);
                        endPic2 = endUnix(todayTime,0,-1);
                    }
                    restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                }
            }
            $('#myselect li').on('click',function(){
                linenum =$(this).data("time"); 
                $(this).parent().parent().find('.set-time').text($(this).text());
                checkType(linenum)
            })
            $('#myselect1 li').on('click',function(){
                picnum1 =$(this).data("time");
                console.log(picnum1)
                $(this).parent().parent().find('.set-time1').text($(this).text());
                checkType1(picnum1)
            })
            $('#myselect2 li').on('click',function(){
                picnum2 =$(this).data("time");
                $(this).parent().parent().find('.set-time2').text($(this).text());
                checkType2(picnum2);
            })
            function echarts(diagramx, diagramy1, diagramy2, pie3, pie1, pie2) {
                var diagramoption = {
                    // title : {
                    //     text: '某楼盘销售情况',
                    //     subtext: '纯属虚构'
                    // },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['拨打', '接通']
                    },
                    calculable: true,
                    xAxis: [{
                        type: 'category',
                        boundaryGap: false,
                        data: diagramx
                    }],
                    yAxis: [{
                        type: 'value'
                    }],
                    series: [{
                            name: '拨打',
                            type: 'line',
                            smooth: true,
                            itemStyle: {
                                normal: {
                                    areaStyle: {
                                        color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                            offset: 0,
                                            color: 'rgba(137,200,227,1)'
                                        }, {
                                            offset: 1,
                                            color: 'rgba(255,255,255,0)'
                                        }]),
                                        type: 'default'
                                    },
                                    color: "#2ec7c9",
                                    lineStyle: {
                                        color: "#2ec7c9"
                                    }
                                }
                            },
                            data: diagramy1
                        },
                        {
                            name: '接通',
                            type: 'line',
                            smooth: true,
                            itemStyle: {
                                normal: {
                                    areaStyle: {
                                        color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                            offset: 0,
                                            color: '#2C609B'
                                        }, {
                                            offset: 1,
                                            color: 'rgba(255,255,255,0)'
                                        }]),
                                        type: 'default'
                                    },
                                    color: "#2C609B",
                                    lineStyle: {
                                        color: "rgba(44,96,155,0.5)"
                                    }
                                }
                            },
                            data: diagramy2
                        }
                    ]
                };
                diagramChart.setOption(diagramoption);
                var piechartoption = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    series: [{
                        name: '客户类型',
                        type: 'pie',
                        data: [{
                                value: pie1,
                                name: 'B类客户'
                            },
                            {
                                value: pie2,
                                name: 'C类客户'
                            },
                            {
                                value: pie3,
                                name: 'A类客户'
                            }
                        ],
                        color: ['rgba(99,143,238,1)', 'rgba(199,203,237,1)', 'rgba(150,143,218,1)'],
                        itemStyle: {
                            normal: {
                                borderWidth: 1,
                                borderColor: '#ffffff',
                            }
                        }
                    }]
                };
                piechartChart.setOption(piechartoption);
            }
            function gettimeFn(a){
              return new Date(a.replace(/-/g,'/')).getTime()/1000;
            }
            function dashtime(url){
                $.get(url, function (data) {
                    // console.log(data)
                    $('.line-num').text(data.running_line + '/' + data.line_num)
                    $('.user_num').text(data.user_num)
                    $('.total_calls').text(data.total_calls)
                    $('.success_calls').text(data.success_calls)
                    $('.fail_calls').text(data.fail_calls)
                    $('.a_calls').text(data.A_calls)
                    $('.b_calls').text(data.B_calls)
                    $('.a-num').text(data.A_num)
                    $('.b-num').text(data.B_num)
                    $('.c-num').text(data.C_num)
                    echarts(data.pic_time, data.pic_call, data.pic_success, data.A_num, data.B_num, data.C_num);
                }, 'json');
            }
            window.addEventListener('resize', function () {
                diagramChart.resize();
                piechartChart.resize();
            });
            var date = new Date();
            var getMonth=date.getMonth()+1;
            if(getMonth<10){
                getMonth='0'+getMonth;
            }
            var getDate=date.getDate();
            if(getDate<10){
                getDate='0'+getDate;
            }
            var datelayui=date.getFullYear()+'-'+getMonth+'-'+getDate;
            layui.use('laydate', function datatime() {
                var laydate = layui.laydate;
                //日期时间
                laydate.render({
                    elem: '#start',
                    type: 'date'
                    ,min: '2018-07-01'
                    ,max: datelayui,
                    btns: ['clear', 'confirm'],
                    calendar: true,
                    done: function (value, date, endDate) {
                        $('#stop').val('');
                        end.config.min = {
                            year: date.year,
                            month: date.month - 1,
                            date: date.date,
                            hours: date.hours,
                            minutes: date.minutes,
                            seconds: date.seconds
                        }
                    }
                });
                var end = laydate.render({
                    elem: '#stop',
                    type: 'date'
                    ,max: datelayui,
                    calendar: true,
                    btns: ['clear', 'confirm'],
                    done: function(value, date){
                        // $("#myselect option[value='today_line']").prop("selected", true); 
                        startline = gettimeFn($('#start').val());
                        endline = gettimeFn($('#stop').val());
                        restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                    }
                });

                laydate.render({
                    elem: '#start1',
                    type: 'date'
                    ,min: '2018-07-01'
                    ,max: datelayui,
                    btns: ['clear', 'confirm'],
                    calendar: true,
                    done: function (value, date, endDate) {
                        $('#stop1').val('');
                        end1.config.min = {
                            year: date.year,
                            month: date.month - 1,
                            date: date.date,
                            hours: date.hours,
                            minutes: date.minutes,
                            seconds: date.seconds
                        }
                    }
                });
                var end1 = laydate.render({
                    elem: '#stop1',
                    type: 'date',
                    min: '2018-07-01',
                    max: datelayui,
                    calendar: true,
                    btns: ['clear', 'confirm'],
                    done: function(value, date){
                        startPic = gettimeFn($('#start1').val());
                        endPic = gettimeFn($('#stop1').val());
                        restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                    }
                });

                laydate.render({
                    elem: '#start2',
                    type: 'date'
                    ,max: datelayui,
                    btns: ['clear', 'confirm'],
                    calendar: true,
                    done: function (value, date, endDate) {
                        $('#stop2').val('');
                        end2.config.min = {
                            year: date.year,
                            month: date.month - 1,
                            date: date.date,
                            hours: date.hours,
                            minutes: date.minutes,
                            seconds: date.seconds
                        }
                    }
                });
                var end2 = laydate.render({
                    elem: '#stop2',
                    type: 'date',
                    max: datelayui,
                    calendar: true,
                    btns: ['clear', 'confirm'],
                    done: function(value, date){
                        startPic2 = gettimeFn($('#start2').val());
                        endPic2 = gettimeFn($('#stop2').val());
                        restEharts(startline,endline,startPic,endPic,startPic2,endPic2);
                    }
                });
            });
        }
    };

    return Controller;
});